package boot.demo.test;

import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;

public class Jdk8New {
    public static void main(String[] args){
        // lambda表达式
        // 函数接口
        // 方法引用
        // stream api
        Map<String, ProductCreditApply> m1 = new HashMap();
        ProductCreditApply prods_01 = (prd) -> { return "I am " + prd;};
        m1.put("PROD-01", prods_01);
        ProductCreditApply prods_02 = (prd) -> { return "We are " + prd;};
        m1.put("PROD-02", prods_02);
        Optional<Map<String, ProductCreditApply>> opt = Optional.ofNullable(m1);
        if (opt.isPresent()) {
            System.out.println(opt.get().get("PROD-02").creditApply("HQ"));
        }

        Map<String, Function<String, String>> m2 = new HashMap();
        m2.put("PROD-02", (productId) -> {return "She is " + productId;});
        m2.put("X-01", (productId) -> {return "You are " + productId;});
        m2.put("X-02", (productId) -> {return "You are " + productId;});
        m2.put("Y-01", (productId) -> {return "He is " + productId;});
        m2.put("Y-02", (productId) -> {return "It is " + productId;});
        System.out.println(m2.get("Y-02").apply("Mobile"));

        List<Plan> plans = new ArrayList<>();
        Plan plan = new Plan();
        plan.setTime(LocalDate.now());
        plan.setToDo("Study jdk lambda and new");
        plans.add(plan);

        Plan plan1 = new Plan();
        plan.setTime(LocalDate.of(2022,01,28));
        plan.setToDo("Goto sleep.");
        plans.add(plan1);

        Plan plan2 = new Plan();
        plan.setTime(LocalDate.of(2022,02,02));
        plan.setToDo("Happy new year!");
        plans.add(plan2);

        Plan plan3 = new Plan();
        plan.setTime(LocalDate.of(2022,02,02));
        plan.setToDo("Happy new year!");
        plans.add(plan3);



    }

    @Data
    public static class Plan {
        private LocalDate time;
        private String toDo;
    }

    @FunctionalInterface
    public interface ProductCreditApply{
        String creditApply(String name);
    }
}
